package com.swift.utils;

import android.text.TextUtils;
import android.util.Log;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

	// 默认日期格式
	public static final String DATEFORMAT = "yyyy-MM-dd HH:mm";
	public static final String YMDHMS_FORMAT = "yyyy-MM-dd HH:mm:ss";
	public static final String YMD_FORMAT = "yyyy-MM-dd";
	public static final String YM_FORMAT = "yyyy-MM";

	/**
	 * 返回unix时间戳 (1970年至今的秒数)
	 * 
	 * @return
	 */
	public static long getUnixStamp() {
		return System.currentTimeMillis() / 1000;
	}

	/**
	 * 得到昨天的日期
	 * 
	 * @return
	 */
	public static String getYestoryDate() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, -1);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String yestoday = sdf.format(calendar.getTime());
		return yestoday;
	}

	/**
	 * 得到今天的日期
	 * 
	 * @return
	 */
	public static String getTodayDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String date = sdf.format(new Date());
		return date;
	}

	/**
	 * 时间戳转化为时间格式
	 * 
	 * @param timeStamp
	 * @return
	 */
	public static String timeStampToStr(long timeStamp) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date = sdf.format(timeStamp * 1000);
		Log.e("timeStampToStr:", date);
		return date;
	}

	public static String timeStampToStr(long timeStamp,String fmt) {
		SimpleDateFormat sdf = new SimpleDateFormat(fmt);
		String date = sdf.format(timeStamp * 1000);
		Log.e("timeStampToStr:", date);
		return date;
	}
	
	/* 
     * 将时间戳转换为时间
     */
    public static String stampToDate(String s){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(s);
        Date date = new Date(lt * 1000);
        res = simpleDateFormat.format(date);
        return res;
    }

	/**
	 * 得到日期 yyyy-MM-dd
	 * 
	 * @param timeStamp
	 *            时间戳
	 * @return
	 */
	public static String formatDate(long timeStamp) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String date = sdf.format(timeStamp * 1000);
		return date;
	}

	/**
	 * 得到时间 HH:mm:ss
	 * 
	 * @param timeStamp
	 *            时间戳
	 * @return
	 */
	public static String getTime(long timeStamp) {
		String time = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date = sdf.format(timeStamp * 1000);
		String[] split = date.split("\\s");
		if (split.length > 1) {
			time = split[1];
		}
		return time;
	}

	/**
	 * 将一个时间戳转换成提示性时间字符串，如刚刚，1秒前
	 * 
	 * @param timeStamp
	 * @return
	 */
	public static String convertTimeToFormat(long timeStamp) {
		long curTime = System.currentTimeMillis() / (long) 1000;
		long time = curTime - timeStamp;

		if (time < 60 && time >= 0) {
			return "刚刚";
		} else if (time >= 60 && time < 3600) {
			return time / 60 + "分钟前";
		} else if (time >= 3600 && time < 3600 * 24) {
			return time / 3600 + "小时前";
		} else if (time >= 3600 * 24 && time < 3600 * 24 * 30) {
			return time / 3600 / 24 + "天前";
		} else if (time >= 3600 * 24 * 30 && time < 3600 * 24 * 30 * 12) {
			return time / 3600 / 24 / 30 + "个月前";
		} else if (time >= 3600 * 24 * 30 * 12) {
			return time / 3600 / 24 / 30 / 12 + "年前";
		} else {
			return "刚刚";
		}
	}
	
	/**   
	 * @author: HYM 
	 * @Title: convertTimeToFormat   
	 * @Description: TODO(这里用一句话描述这个方法的作用)   
	 * @param datetimeString
	 * @return String 
	 * @date: 2017-7-4
	 */  
	public static String convertTimeToFormat(String datetimeString) {
		Date temp = fmtStrToDate(datetimeString, YMDHMS_FORMAT);
		return convertTimeToFormat(temp.getTime()/ (long) 1000);
	}

	/**
	 * 将一个时间戳转换成提示性时间字符串，(多少分钟)
	 * 
	 * @param timeStamp
	 * @return
	 */
	public static String timeStampToFormat(long timeStamp) {
		long curTime = System.currentTimeMillis() / (long) 1000;
		long time = curTime - timeStamp;
		return time / 60 + "";
	}

	public static Date fmtStrToDate(String date, String dtFormat) {
		try {
			SimpleDateFormat dateFormat = new SimpleDateFormat(dtFormat);
			return dateFormat.parse(date);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static boolean compare(String date1, String date2, String format) {
		// String s1="2008-01-25 09:12:09";
		// String s2="2008-01-29 09:12:11";
		java.text.DateFormat df = new java.text.SimpleDateFormat(format);
		java.util.Calendar c1 = java.util.Calendar.getInstance();
		java.util.Calendar c2 = java.util.Calendar.getInstance();
		try {
			c1.setTime(df.parse(date1));
			c2.setTime(df.parse(date2));
		} catch (java.text.ParseException e) {
			System.err.println("格式不正确");
			return true;
		}
		int result = c1.compareTo(c2);
		if (result == 0)
			System.out.println("c1相等c2");
		else if (result < 0)
			System.out.println("c1小于c2");
		else
			System.out.println("c1大于c2");
		return result >= 0;
	}

	/**
	 * 把时间串格式格成年月日
	 * 
	 * @param datetime
	 *            2016-05-07 14:55:20
	 * @return 2016-05-07
	 * @author 吴进 by 20160507
	 */
	public static String getBuildDateString(String datetime) {
		if (datetime != null && datetime.length() > 0) {
			String[] strs = datetime.trim().split("\\s");
			if (strs != null) {
				return strs[0].trim();
			}
		}
		return null;
	}

	/**
	 * Description:格式化日期,String字符串转化为Date
	 * 
	 * @param date
	 * @param dtFormat
	 *            例如:yyyy-MM-dd HH:mm:ss yyyyMMdd
	 * @return string
	 */
	public static String fmtDateToStr(Date date, String dtFormat) {
		if (date == null)
			return "";
		try {
			SimpleDateFormat dateFormat = new SimpleDateFormat(dtFormat);
			return dateFormat.format(date);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
	public static String fmtDateToStr(String date, String dtFormat) {
		if (TextUtils.isEmpty(date))
			return "";
		try {
			SimpleDateFormat dateFormat = new SimpleDateFormat(dtFormat);
			return dateFormat.format(getStringToDate(date,YMD_FORMAT));
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 指定时间串格式化日期
	 * 
	 * @param input
	 * @param pattern
	 * @return
	 * @throws ParseException
	 */
	public static Date getStringToDate(String input, String pattern)
			throws ParseException {
		if (input == null || pattern == null) {
			return null;
		} else {
			DateFormat df = new SimpleDateFormat(pattern);
			return df.parse(input);
		}
	}

	/**
	 * author hym description 获取几分钟后的时间 格式Long
	 * 
	 * @param min
	 * @return
	 */
	public static long getAfterMin(int min) {
		Calendar nowTime = Calendar.getInstance();
		nowTime.add(Calendar.MINUTE, min);
		return nowTime.getTimeInMillis();
	}

	public static Date getAfterMinToDate(int min) {
		Calendar nowTime = Calendar.getInstance();
		nowTime.add(Calendar.MINUTE, min);
		return nowTime.getTime();
	}

	public static long[] setCountDownTime(Date b, Date e) throws ParseException {
		long[] result;
		long l = e.getTime() - b.getTime();
		if (l > 0) {
			long day = l / (24 * 60 * 60 * 1000);
			long hour = (l / (60 * 60 * 1000) - day * 24);
			long minute = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
			long second = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60);
			result = new long[] { day, hour, minute, second };
		} else {
			result = new long[] { 0, 0, 0, 0 };
		}
		return result;
	}

	/**
	 * author hym description 加减月份
	 * 
	 * @param date
	 * @param dateformat
	 * @param month
	 * @return
	 * @throws ParseException
	 */
	public static String operaMonth(String date, String dateformat, int month)
			throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(dateformat);
		Date dt = sdf.parse(date);
		Calendar rightNow = Calendar.getInstance();
		rightNow.setTime(dt);
		rightNow.add(Calendar.MONTH, month);// 日期加3个月
		Date dt1 = rightNow.getTime();
		String reStr = sdf.format(dt1);
		return reStr;
	}

	/**
	 * 格式化时间
	 * 
	 * @param longTime
	 *            时间
	 * @return 格式后的时间
	 */
	public static String formatTimeFString(long longTime) {
		String time = "%2d:%2d:%2d";
		int hour = parseLongToInt(longTime / (60 * 60));
		int min = parseLongToInt((longTime - hour * (60L * 60)) / 60);
		int sec = parseLongToInt(longTime % 60);
		time = String.format(time, hour, min, sec);

		return time.replace(' ', '0');
	}

	public static int parseLongToInt(long value) {
		return Long.valueOf(value).intValue();
	}
	
	/**
	 * 传进来的time的格式要跟"yyyy-MM-dd HH:mm"一致
	 * */
	public static Date getDateFromTime(String time) {
		if (!TextUtils.isEmpty(time)) {
			DateFormat startdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			Date date;
			try {
				date = startdf.parse(time);
			} catch (ParseException e) {
				date = null;
			}
			return date;
		} else {
			return null;
		}

	}
	
	/**   
	 * @author: HYM 
	 * @Title: groupByMouth   
	 * @Description: TODO(根据给出的年月来格式化日期，按月，如3月，2017年4月)   
	 * @param yyyyMM
	 * @return String 
	 * @date: 2017-8-4
	 */  
	public static String groupByMouth(String yyyyMM)
	{
		StringBuilder result = new StringBuilder();
		String year = yyyyMM.substring(0,4);
		String mouth = yyyyMM.substring(4,6);
		Calendar calendar = Calendar.getInstance();
		int curYear = calendar.get(Calendar.YEAR);
		int curMouth = (calendar.get(Calendar.MONTH)+1);
		if (Integer.parseInt(year) == curYear) {
			if (Integer.parseInt(mouth) == curMouth) {
				result.append("本月");
			}
			else {
				result.append(Integer.parseInt(mouth) + "月");
			}
		}
		else {
			result.append(Integer.parseInt(year)+"年"+Integer.parseInt(mouth) + "月");
		}
		return result.toString();
	}
}